package com.sec.etech.employeesync.confighandler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sec.etech.employeesync.entity.OrgEmployeeZNationality;
import com.sec.etech.employeesync.mapper.OrgEmployeeZNationalityMapper;
import com.sec.etech.employeesync.util.ConfigTableHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class NationalityConfigHandler implements ConfigTableHandler<OrgEmployeeZNationality> {

    @Resource
    private OrgEmployeeZNationalityMapper nationalityMapper;

    @Override
    public Long getOrCreateConfigId(String... params) {
        String code = params[0];
        String name = params[1];
        OrgEmployeeZNationality nationality = getEntityByName(code);
        if (nationality == null) {
//            nationality = new OrgEmployeeZNationality();
//            nationality.setCode(code);
//            nationality.setName(name.split(",")[0]);
//            nationality.setActive(1);
//            insertEntity(nationality);
//            log.info("新增国籍配置:name={}", name);
            return null;
        }
        return nationality.getNationalityId();
    }

    @Override
    public OrgEmployeeZNationality getEntityByName(String code) {
        return nationalityMapper.selectOne(Wrappers.<OrgEmployeeZNationality>lambdaQuery()
                .eq(OrgEmployeeZNationality::getCode, code));
    }

    @Override
    public String generateNextCode() {
        return null;
    }

    @Override
    public void insertEntity(OrgEmployeeZNationality entity) {
        nationalityMapper.insert(entity);
    }
}
